home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-17 | 14.9 KB | 576 lines | [TEXT/KAHL] |
- #include "menus.h"
- #include "main.h"
- #include "interface.h"
- #include "window.h"
-
- extern short selUndo;
- extern WindowPtr pwinUndo;
-
- short nCurrentFont; // menu item of currently checked font, used to uncheck
- short nDefaultFont; // menu item of font when no application window is open
- short nCurrentSize;
- short nDefaultSize;
- short nCurrentVoice;
- short nDefaultVoice;
-
- const short cSizes = 10;
- short sizes[cSizes] = { 0, 9, 10, 12, 14, 18, 24, 36, 48, 72 };
-
- // return the font number that is item number nItem in the Font menu
- short FontNumber(int nItem) {
- MenuHandle hmenu = GetMenuHandle(MENU_FONT);
- short nFont = 0;
- if (hmenu) {
- Str255 sFontName;
- GetMenuItemText(hmenu, nItem, sFontName);
- GetFNum(sFontName, &nFont);
- }
- return nFont;
- } // FontNumber
-
- // return item number of the item in the Font menu for font nFont, 0 if item not present
- short FontItem(int nFont) {
- MenuHandle hmenu = GetMenuHandle(MENU_FONT);
- short nItem = 0;
- if (hmenu) {
- short cItem = CountMItems(hmenu);
- for (short i = 1; i < cItem+1; i++)
- if (nFont == FontNumber(i))
- nItem = i;
- }
- return nItem;
- } // FontItem
-
- void AdjustFonts(void) {
- MenuHandle hmenu = GetMenuHandle(MENU_FONT);
- if (hmenu) {
- CheckItem(hmenu, nCurrentFont, 0);
- WindowPtr pwin = FrontWindow();
- short windowKind;
- if (pwin) {
- windowKind = GetWindowKind(pwin);
- if (windowKind == userKind) {
- CGrafPtr pPort = GetWindowPort(pwin);
- short nFont = FontItem(pPort->txFont);
- nCurrentFont = nFont;
- } else
- nCurrentFont = nDefaultFont;
- } else
- nCurrentFont = nDefaultFont;
- if (pwin && windowKind==userKind)
- CheckItem(hmenu, nCurrentFont, 1);
- else
- SetItemMark(hmenu, nCurrentFont, diamondMark);
- }
- } // AdjustFonts
-
- short SizeNumber(short nItem) {
- short nSize;
- if (nItem>=1 && nItem<cSizes)
- nSize = sizes[nItem];
- else
- nSize = 0;
- return nSize;
- } // SizeNumber
-
- short SizeItem(short nSize) {
- short nItem = 0;
- for (short i=1; i<cSizes; i++)
- if (sizes[i] == nSize) {
- nItem = i;
- break;
- }
- return nItem;
- } // SizeItem
-
- void AdjustSize(void) {
- MenuHandle hmenu = GetMenuHandle(MENU_SIZE);
- if (hmenu) {
- CheckItem(hmenu, nCurrentSize, 0);
- WindowPtr pwin = FrontWindow();
- short windowKind;
- if (pwin) {
- windowKind = GetWindowKind(pwin);
- if (windowKind == userKind) {
- CGrafPtr pPort = GetWindowPort(pwin);
- short nSize = SizeItem(pPort->txSize);
- nCurrentSize = nSize;
- } else
- nCurrentSize = nDefaultSize;
- } else
- nCurrentSize = nDefaultSize;
- if (pwin && windowKind==userKind)
- CheckItem(hmenu, nCurrentSize, 1);
- else
- SetItemMark(hmenu, nCurrentSize, diamondMark);
- short nFont = FontNumber(nCurrentFont);
- for (short i=1; i<cSizes; i++)
- if (RealFont(nFont, sizes[i]))
- SetItemStyle(hmenu, i, outline);
- else
- SetItemStyle(hmenu, i, 0);
- }
- } // AdjustSize
-
- void AdjustQuotes(void) {
- MenuHandle hmenu = GetMenuHandle(MENU_QUOTES);
- if (hmenu) {
- WindowPtr pwin = FrontWindow();
- if (pwin) {
- short windowKind = GetWindowKind(pwin);
- if (windowKind < 0)
- DisableItem(hmenu, 0);
- else {
- EnableItem(hmenu, 0);
- QuotesWindow *pqw = (QuotesWindow *)GetWRefCon(pwin);
- int iCurrent = pqw->current();
- int count = pqw->count();
- if (iCurrent == 0) {
- DisableItem(hmenu, QUOTES_FIRST);
- DisableItem(hmenu, QUOTES_PREVIOUS);
- } else {
- EnableItem(hmenu, QUOTES_FIRST);
- EnableItem(hmenu, QUOTES_PREVIOUS);
- }
- if (iCurrent == count-1) {
- DisableItem(hmenu, QUOTES_NEXT);
- DisableItem(hmenu, QUOTES_LAST);
- } else {
- EnableItem(hmenu, QUOTES_NEXT);
- EnableItem(hmenu, QUOTES_LAST);
- }
- EnableItem(hmenu, QUOTES_GOTO);
- }
- } else
- DisableItem(hmenu, 0);
- }
- } // AdjustQuotes
-
- short cVoices;
- short *pVoices;
- void SetDefaultVoice(void) {
- nDefaultVoice = 1;
- MenuHandle hmenu = GetMenuHandle(MENU_SPEECH);
- if ( (**hmenu).enableFlags & 1) {
- VoiceDescription VD;
- VD.length = sizeof(VoiceDescription);
- GetVoiceInfo(NULL, soVoiceDescription, &VD);
- VoiceSpec VS;
- for (short i=1; i<=cVoices; i++) {
- GetIndVoice(pVoices[i], &VS);
- if (VD.voice.creator==VS.creator && VD.voice.id==VS.id) {
- nDefaultVoice = i;
- break;
- }
- }
- }
- nCurrentVoice = nDefaultVoice;
- } // SetDefaultVoice
-
- void SortVoices(MenuHandle hmenu) {
- if (hmenu) {
- short i, j;
- Str255 sT, sTLoop, sTCopy;
- short iT;
- for (i=2; i<=cVoices; i++) {
- GetMenuItemText(hmenu, i, sT);
- iT = pVoices[i];
- j = i;
- GetMenuItemText(hmenu, j-1, sTLoop);
- while (j>1 && StringOrder(sTLoop, sT, smSystemScript, smSystemScript, systemDefLang, systemDefLang)==1) { // sTLoop>sT
- GetMenuItemText(hmenu, j-1, sTCopy);
- SetMenuItemText(hmenu, j, sTCopy);
- pVoices[j] = pVoices[j-1];
- j--;
- if (j>1)
- GetMenuItemText(hmenu, j-1, sTLoop);
- }
- SetMenuItemText(hmenu, j, sT);
- pVoices[j] = iT;
- }
- }
- } // SortVoices
-
- void SetupVoices(void) {
- long response;
- OSErr err = Gestalt(gestaltSpeechAttr, &response);
- short fHasSpeech;
- if (err == noErr)
- fHasSpeech = response & (1<<gestaltSpeechMgrPresent);
- else
- fHasSpeech = 0;
- if (fHasSpeech) {
- MenuHandle hmenu;
- hmenu = GetMenu(MENU_VOICES);
- err = CountVoices(&cVoices);
- pVoices = (short *)NewPtr(cVoices*sizeof(short));
- if (err == noErr) {
- VoiceSpec VS;
- VoiceDescription VD;
- long lenVD;
- for (int i = 1; i <= cVoices; i++) {
- err = GetIndVoice(i, &VS);
- if (err == noErr) {
- err = GetVoiceDescription(&VS, &VD, sizeof(VoiceDescription));
- if (err == noErr) {
- AppendMenu(hmenu, VD.name);
- pVoices[i] = i;
- }
- }
- }
- }
- InsertMenu(hmenu, -1);
- SortVoices(hmenu);
- SetDefaultVoice();
- hmenu = GetMenuHandle(MENU_SPEECH);
- if (hmenu)
- SetItemMark(hmenu, SPEECH_VOICES, (char)MENU_VOICES);
- } else
- cVoices = 0;
- } // SetupVoices
-
- void AdjustWindows(void) {
- int fWindows7; // Help item is in the Help menu
- MenuHandle hmenu = GetMenuHandle(MENU_WINDOW_7);
- if (hmenu == NULL) {
- hmenu = GetMenuHandle(MENU_WINDOW_6);
- fWindows7 = 0;
- } else
- fWindows7 = 1;
- if (hmenu) {
- // clear the menu
- int cItems;
- if (fWindows7) {
- cItems = CountMItems(hmenu);
- for (int i = 0; i < cItems; i++)
- DeleteMenuItem(hmenu, 1);
- } else {
- cItems = CountMItems(hmenu) - 2;
- for (int i = 0; i < cItems; i++)
- DeleteMenuItem(hmenu, 3);
- }
- // build the menu
- Str255 sWindow;
- short windowKind;
- WindowPtr pwin = FrontWindow();
- while (pwin) {
- windowKind = GetWindowKind(pwin);
- if (windowKind == userKind) {
- GetWTitle(pwin, sWindow);
- AppendMenu(hmenu, sWindow);
- }
- pwin = GetNextWindow(pwin);
- }
- pwin = FrontWindow();
- if (pwin) {
- windowKind = GetWindowKind(pwin);
- if (windowKind < 0)
- EnableItem(hmenu, 0);
- else if (windowKind == userKind) {
- EnableItem(hmenu, 0);
- if (fWindows7)
- CheckItem(hmenu, 1, 1);
- else
- CheckItem(hmenu, 3, 1);
- } else if (windowKind == dialogKind)
- DisableItem(hmenu, 0);
- } else
- if (fWindows7)
- DisableItem(hmenu, 0);
- else
- EnableItem(hmenu, 0);
- }
- } // AdjustWindows
-
- int SetupMenus(void) {
- long response;
- OSErr err = Gestalt(gestaltHelpMgrAttr, &response);
- int fHasHelpMgr;
- if (err == noErr)
- fHasHelpMgr = response & (1<<gestaltHelpMgrPresent);
- else
- fHasHelpMgr = 0;
- Handle hmenus;
- if (fHasHelpMgr)
- hmenus = GetNewMBar(MENUS_7);
- else
- hmenus = GetNewMBar(MENUS_6);
- if (hmenus) {
- SetMenuBar(hmenus);
- DisposHandle(hmenus);
- MenuHandle hmenu = GetMenuHandle(MENU_APPLE);
- if (hmenu)
- AppendResMenu(hmenu, 'DRVR');
- hmenu = GetMenuHandle(MENU_FONT);
- if (hmenu) {
- AppendResMenu(hmenu, 'FONT');
- Str255 sFontName;
- GetFontName(1, sFontName); // 1 is the system default application font number
- short nFont;
- GetFNum(sFontName, &nFont);
- nCurrentFont = FontItem(nFont);
- nDefaultFont = FontItem(nFont);
- }
- if (fHasHelpMgr) {
- Str255 itemString;
- GetIndString(itemString, MENU_STRINGS, STRING_HELP);
- err = HMGetHelpMenuHandle(&hmenu);
- if (err == noErr)
- AppendMenu(hmenu, itemString);
- }
- SetupVoices();
- AdjustMenus();
- return noErr;
- } else
- return memFullErr;
- } // SetupMenus
-
- void AdjustVoices(void) {
- MenuHandle hmenu = GetMenuHandle(MENU_VOICES);
- if (hmenu) {
- CheckItem(hmenu, nCurrentVoice, 0);
- WindowPtr pwin = FrontWindow();
- if (pwin) {
- short windowKind = GetWindowKind(pwin);
- if (windowKind < 0)
- nCurrentVoice = nDefaultVoice;
- else {
- QuotesWindow *pqw = (QuotesWindow *)GetWRefCon(pwin);
- for (short i=1; i<=cVoices; i++)
- if (pVoices[i] == pqw->voice) {
- nCurrentVoice = i;
- break;
- }
- }
- } else
- nCurrentVoice = nDefaultVoice;
- if (pwin)
- CheckItem(hmenu, nCurrentVoice, 1);
- else
- SetItemMark(hmenu, nCurrentVoice, diamondMark);
- }
- } // AdjustVoices
-
- void AdjustSpeech(void) {
- MenuHandle hmenuSpeech = GetMenuHandle(MENU_SPEECH);
- WindowPtr pwin = FrontWindow();
- long response;
- OSErr err = Gestalt(gestaltSpeechAttr, &response);
- int fHasSpeech;
- if (err == noErr)
- fHasSpeech = response & (1<<gestaltSpeechMgrPresent);
- else
- fHasSpeech = 0;
- MenuHandle hmenuVoices = GetMenuHandle(MENU_VOICES);
- if (fHasSpeech) {
- EnableItem(hmenuSpeech, 0);
- if (SpeechBusy()) {
- DisableItem(hmenuSpeech, SPEECH_SPEAK_ALL);
- EnableItem(hmenuSpeech, SPEECH_STOP_SPEAKING);
- DisableItem(hmenuVoices, 0);
- } else {
- if (pwin)
- EnableItem(hmenuSpeech, SPEECH_SPEAK_ALL);
- else
- DisableItem(hmenuSpeech, SPEECH_SPEAK_ALL);
- DisableItem(hmenuSpeech, SPEECH_STOP_SPEAKING);
- EnableItem(hmenuVoices, 0);
- }
- AdjustVoices();
- } else
- DisableItem(hmenuSpeech, 0);
- } // AdjustSpeech
-
- void AdjustUndo(void) {
- MenuHandle hmenuEdit = GetMenuHandle(MENU_EDIT);
- if (hmenuEdit) {
- if (selUndo == CANT_UNDO)
- DisableItem(hmenuEdit, EDIT_UNDO);
- else {
- WindowPtr pwin = FrontWindow();
- if (pwin) {
- short windowKind = GetWindowKind(pwin);
- if (windowKind < 0)
- EnableItem(hmenuEdit, EDIT_UNDO);
- else {
- if (pwin == pwinUndo)
- EnableItem(hmenuEdit, EDIT_UNDO);
- else
- DisableItem(hmenuEdit, EDIT_UNDO);
- }
- } else
- if (pwin == pwinUndo)
- EnableItem(hmenuEdit, EDIT_UNDO);
- else
- DisableItem(hmenuEdit, EDIT_UNDO);
- }
- Str255 sItem;
- GetIndString(sItem, MENU_STRINGS, selUndo);
- if (selUndo == CANT_UNDO || selUndo == UNDO)
- SetMenuItemText(hmenuEdit, 1, sItem);
- else
- SetMenuItemText(hmenuEdit, 1, sItem);
- }
- } // AdjustUndo
-
- void AdjustHelpMenu(void) {
- long response;
- OSErr err = Gestalt(gestaltHelpMgrAttr, &response);
- int fHasHelpMgr;
- if (err == noErr)
- fHasHelpMgr = response & (1<<gestaltHelpMgrPresent);
- else
- fHasHelpMgr = 0;
- Handle hmenus;
- if (fHasHelpMgr) {
- MenuHandle hmenuHelp;
- OSErr err = HMGetHelpMenuHandle(&hmenuHelp);
- if (err == noErr) {
- short cItems = CountMItems(hmenuHelp);
- WindowPtr pwin = FrontWindow();
- if (pwin) {
- short windowKind = GetWindowKind(pwin);
- if (windowKind == dialogKind)
- DisableItem(hmenuHelp, cItems);
- else
- EnableItem(hmenuHelp, cItems);
- } else
- EnableItem(hmenuHelp, cItems);
- }
- }
- } // AdjustHelpMenu
-
- void AdjustMenus(void) {
- WindowPtr pwin = FrontWindow();
- short windowKind;
- if (pwin)
- windowKind = GetWindowKind(pwin);
- MenuHandle hmenuApple = GetMenuHandle(MENU_APPLE);
- MenuHandle hmenuFile = GetMenuHandle(MENU_FILE);
- MenuHandle hmenuEdit = GetMenuHandle(MENU_EDIT);
- MenuHandle hmenuQuotes = GetMenuHandle(MENU_QUOTES);
- MenuHandle hmenuSpeech = GetMenuHandle(MENU_SPEECH);
- MenuHandle hmenuFont = GetMenuHandle(MENU_FONT);
- MenuHandle hmenuSize = GetMenuHandle(MENU_SIZE);
- short fFileEnabled = (**hmenuFile).enableFlags & 1;
- short fEditEnabled = (**hmenuEdit).enableFlags & 1;
- short fQuotesEnabled = (**hmenuQuotes).enableFlags & 1;
- if (pwin) {
- EnableItem(hmenuFile, FILE_CLOSE);
- EnableItem(hmenuEdit, 0);
- if (windowKind < 0) {
- EnableItem(hmenuApple, 0);
- EnableItem(hmenuFile, 0);
- DisableItem(hmenuFile, FILE_SAVE);
- DisableItem(hmenuFile, FILE_SAVE_AS);
- EnableItem(hmenuEdit, EDIT_CUT);
- EnableItem(hmenuEdit, EDIT_COPY);
- EnableItem(hmenuEdit, EDIT_PASTE);
- EnableItem(hmenuEdit, EDIT_CLEAR);
- DisableItem(hmenuEdit, EDIT_SELECT_ALL);
- DisableItem(hmenuQuotes, 0);
- DisableItem(hmenuSpeech, 0);
- DisableItem(hmenuFont, 0);
- DisableItem(hmenuSize, 0);
- } else if (windowKind == dialogKind) {
- DisableItem(hmenuApple, 0);
- DisableItem(hmenuFile, 0);
- short selDialog = GetWRefCon(pwin);
- switch (selDialog) {
- case DLOG_ABOUT:
- DisableItem(hmenuEdit, EDIT_CUT);
- EnableItem(hmenuEdit, EDIT_COPY);
- DisableItem(hmenuEdit, EDIT_PASTE);
- DisableItem(hmenuEdit, EDIT_CLEAR);
- DisableItem(hmenuEdit, EDIT_SELECT_ALL);
- break;
- case DLOG_GOTO:
- TEHandle hte = ((DialogPeek)pwin)->textH;
- if ((**hte).selStart == (**hte).selEnd) {
- DisableItem(hmenuEdit, EDIT_CUT);
- DisableItem(hmenuEdit, EDIT_COPY);
- DisableItem(hmenuEdit, EDIT_CLEAR);
- } else {
- EnableItem(hmenuEdit, EDIT_CUT);
- EnableItem(hmenuEdit, EDIT_COPY);
- EnableItem(hmenuEdit, EDIT_CLEAR);
- }
- EnableItem(hmenuEdit, EDIT_PASTE);
- short item;
- Rect r;
- Handle hText;
- GetDialogItem(pwin, 3, &item, &hText, &r);
- Str255 s;
- GetDialogItemText(hText, s);
- if ((**hte).selStart == 0 && (**hte).selEnd == s[0])
- DisableItem(hmenuEdit, EDIT_SELECT_ALL);
- else
- EnableItem(hmenuEdit, EDIT_SELECT_ALL);
- break;
- }
- DisableItem(hmenuQuotes, 0);
- DisableItem(hmenuSpeech, 0);
- DisableItem(hmenuFont, 0);
- DisableItem(hmenuSize, 0);
- } else if (windowKind == userKind) {
- EnableItem(hmenuApple, 0);
- EnableItem(hmenuFile, 0);
- EnableItem(hmenuFile, FILE_SAVE_AS);
- QuotesWindow *pqw = (QuotesWindow *)GetWRefCon(pwin);
- if (pqw->save())
- EnableItem(hmenuFile, FILE_SAVE);
- else
- DisableItem(hmenuFile, FILE_SAVE);
- DisableItem(hmenuEdit, EDIT_CUT);
- if (1) // text selected in quote window
- EnableItem(hmenuEdit, EDIT_COPY);
- else
- DisableItem(hmenuEdit, EDIT_COPY);
- DisableItem(hmenuEdit, EDIT_PASTE);
- DisableItem(hmenuEdit, EDIT_CLEAR);
- if (0) // not all of the text in the window is selected
- EnableItem(hmenuEdit, EDIT_SELECT_ALL);
- else
- DisableItem(hmenuEdit, EDIT_SELECT_ALL);
- EnableItem(hmenuQuotes, 0);
- AdjustQuotes();
- AdjustSpeech();
- EnableItem(hmenuFont, 0);
- EnableItem(hmenuSize, 0);
- }
- } else {
- EnableItem(hmenuApple, 0);
- EnableItem(hmenuFile, 0);
- DisableItem(hmenuFile, FILE_CLOSE);
- DisableItem(hmenuFile, FILE_SAVE);
- DisableItem(hmenuFile, FILE_SAVE_AS);
- if (selUndo == CANT_UNDO)
- DisableItem(hmenuEdit, 0);
- else
- EnableItem(hmenuEdit, 0);
- DisableItem(hmenuEdit, EDIT_CUT);
- DisableItem(hmenuEdit, EDIT_COPY);
- DisableItem(hmenuEdit, EDIT_PASTE);
- DisableItem(hmenuEdit, EDIT_CLEAR);
- DisableItem(hmenuEdit, EDIT_SELECT_ALL);
- DisableItem(hmenuQuotes, 0);
- AdjustSpeech();
- EnableItem(hmenuFont, 0);
- EnableItem(hmenuSize, 0);
- }
- AdjustUndo();
- AdjustFonts();
- AdjustSize();
- AdjustWindows();
- AdjustHelpMenu();
- short fRedraw = 0;
- if (fFileEnabled != ((**hmenuFile).enableFlags & 1))
- fRedraw = 1;
- if (fEditEnabled != ((**hmenuEdit).enableFlags & 1))
- fRedraw = 1;
- if (fQuotesEnabled != ((**hmenuQuotes).enableFlags & 1))
- fRedraw = 1;
- if (fRedraw)
- DrawMenuBar();
- } // AdjustMenus
-